package com.service.admin.base.controller;

import com.service.admin.base.controller.rep.ConfigGroupRep;
import com.service.admin.base.controller.rep.ConfigPageRep;
import com.service.admin.base.controller.rep.ConfigRep;
import com.service.admin.base.controller.req.SysConfigModifyReq;
import com.service.admin.base.service.ConfigService;
import com.service.admin.framework.log.RequestLog;
import com.service.core.exception.CommonException;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.MultiResponse;
import com.service.core.rest.response.OneResponse;
import com.service.dao.constants.ConfigConstants;
import com.service.dao.model.SysConfig;
import com.service.utility.modelmapper.ModelMapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年12月24日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@RestController()
@Api(tags = "配置相关")
@RequestMapping("config")
public class ConfigController extends BaseController {

    @Autowired
    private ConfigService configService;

    @ApiOperation("站点信息配置")
    @RequestLog("站点信息配置")
    @PostMapping("system-info")
    public MultiResponse<ConfigRep> systemInfo() {
        return successQueryMultiResponse(configService.getConfigRepByGroup(ConfigConstants.Group.base.getCode()));
    }

    @ApiOperation("获取配置分组")
    @RequestLog("获取配置分组")
    @GetMapping("list-group")
    public MultiResponse<ConfigGroupRep> listGroup() {
        return successQueryMultiResponse(configService.listGroup());
    }

    @ApiOperation("获取分组详细参数配置")
    @RequestLog("获取分组详细参数配置")
    @GetMapping("{groupName}/list-config")
    public MultiResponse<ConfigPageRep> listGroupConfig(@PathVariable("groupName") ConfigConstants.Group group) {
        return successQueryMultiResponse(configService.listGroupConfig(group.getCode()));
    }

    @ApiOperation("修改配置")
    @RequestLog("修改配置")
    @GetMapping("detail/{id}")
    public OneResponse<ConfigPageRep> detail(@PathVariable("id") Integer id){
        SysConfig sysConfig = configService.getById(id);
        if(ObjectUtils.isEmpty(sysConfig)){
            throw new CommonException("不存在此系统配置");
        }
        return successQueryOneResponse(ModelMapperUtil.map(sysConfig, ConfigPageRep.class));
    }

    @ApiOperation("修改配置")
    @RequestLog("修改配置")
    @PostMapping("modify")
    public BaseResponse modify(@RequestBody @Validated SysConfigModifyReq modifyReq){
        configService.modifyConfig(modifyReq);
        return successUpdateBaseResponse();
    }


}
